package com.mtnz.njk.GexinUtil;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 执行批量推
 * @Author: FBL
 * @CreateDate: 2019\8\29 0029 7:55
 * @Version: 1.0
 */
public class PushList {
    /**
     * @param cIds
     * @param title
     * @param text
     * @param transmissionType    后续动作 ：1 立即启动APP，2：客户端收到消息后需要自行处理
     * @param transmissionContent
     * @param tranStatus          消息类型 1 通知消息 2 透传消息
     * @throws Exception
     */
    public void pushList(List<String> cIds, String title, String text, Integer transmissionType, String transmissionContent, Integer tranStatus) throws Exception {
        // 配置返回每个用户返回用户状态，可选
        System.setProperty("gexin_pushList_needDetails", "true");
        IGtPush push = new IGtPush(GexinConfig.URL, GexinConfig.APP_KEY, GexinConfig.MASTER_SECRET);

        ListMessage message = new ListMessage();
        if (tranStatus == 1) {
            // 使用通知消息模板
            NotificationTemplate template = notificationTemplateDemo(title, text, transmissionType, transmissionContent);
            message.setData(template);
        }
        if (tranStatus == 2) {
            // 使用透传消息模板
            TransmissionTemplate transmissionTemplate = getTransmissionTemplate(transmissionType, transmissionContent);
            message.setData(transmissionTemplate);
        }
        // 设置消息离线，并设置离线时间
        message.setOffline(true);
        // 离线有效时间，单位为毫秒
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 配置推送目标
        List targets = new ArrayList();
        for (String cId : cIds) {
            Target target = new Target();
            target.setAppId(GexinConfig.APP_ID);
            target.setClientId(cId);
            targets.add(target);
        }
        // taskId用于在推送时去查找对应的message
        String taskId = push.getContentId(message);
        IPushResult ret = push.pushMessageToList(taskId, targets);
        System.out.println(ret.getResponse().toString());
    }

    public static NotificationTemplate notificationTemplateDemo(String title, String text, Integer transmissionType, String transmissionContent) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(GexinConfig.APP_ID);
        template.setAppkey(GexinConfig.APP_KEY);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(title);
        style.setText(text);
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        style.setChannel("通知渠道id");
        style.setChannelName("通知渠道名称");
        style.setChannelLevel(3); //设置通知渠道重要性
        template.setStyle(style);

        // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理
        template.setTransmissionType(transmissionType);
        template.setTransmissionContent(transmissionContent);
        return template;
    }

    /**
     * 透传消息模板
     *
     * @param transmissionType
     * @param transmissionContent
     * @return
     */
    public static TransmissionTemplate getTransmissionTemplate(Integer transmissionType, String transmissionContent) {
        TransmissionTemplate transmissionTemplate = new TransmissionTemplate();
        transmissionTemplate.setAppId(GexinConfig.APP_ID);
        transmissionTemplate.setAppkey(GexinConfig.APP_KEY);
        transmissionTemplate.setTransmissionType(transmissionType);
        transmissionTemplate.setTransmissionContent(transmissionContent);
        return transmissionTemplate;
    }
}
